package com.org.cys.util;

import com.org.cys.util.StringUtil;
import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.commons.collections.MapUtils;


/**
 * Map工具类
 * @author　caiyushen
 **/
public class MapUtil extends MapUtils {
	
    public MapUtil() {
		
    }

	/**
	 * 级联获取嵌套map的值
	 * @param map - 嵌套的map对象
	 * @param keyPath - 键路径，以.分隔
	 * */
    public static Object getCascadeValue(Map<String, Object> map, String keyPath) {
        Object value = null;
        if(map != null && keyPath != null) {
            String[] keyList = keyPath.split("\\.");

            for(int i = 0; i < keyList.length && map != null; ++i) {
                String key = keyList[i];
                value = map.get(key);
                if(value instanceof Map) {
                    map = (Map)value;
                } else {
                    map = null;
                }
            }
        }

        return value;
    }

    
    /**
     * 获取map的值为List对象
     * @param map - map对象
     * @param key - 键
     * */
    public static List<Object> getValueAsList(Map<String, Object> map, String key) {
        if(map == null) {
            return null;
        } else {
            List<Object> result = new ArrayList();
            if(map.containsKey(key)) {
                Object value = map.get(key);
                if(value instanceof List) {
                    result = (List)value;
                } else {
                    ((List)result).add(value);
                }
            }

            return (List)result;
        }
    }

    
    /**
     * 将一个 Map 对象转化为一个 JavaBean
     * @param type - 要转化的类型
     * @param map - 包含属性值的 map
     * @return 转化出来的 JavaBean 对象
     * @exception
     * java.beans.IntrospectionException - 如果分析类属性失败
     * java.lang.IllegalAccessException - 如果实例化 JavaBean 失败 
     * java.lang.InstantiationException - 如果实例化 JavaBean 失败 
     * java.lang.reflect.InvocationTargetException - 如果调用属性的 setter 方法失败 
     * */
    public static Object convertMap(Class type, Map map) throws IntrospectionException, IllegalAccessException, InstantiationException, InvocationTargetException {
        BeanInfo beanInfo = Introspector.getBeanInfo(type);
        Object obj = type.newInstance();
        PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();

        for(int i = 0; i < propertyDescriptors.length; ++i) {
            PropertyDescriptor descriptor = propertyDescriptors[i];
            String propertyName = descriptor.getName();
            if(map.containsKey(propertyName)) {
                Object value = map.get(propertyName);
                Object[] args = new Object[]{value};
                descriptor.getWriteMethod().invoke(obj, args);
            }
        }

        return obj;
    }

    
    /**
     * 将一个 JavaBean 对象转化为一个 Map
     * @param bean - 要转化的JavaBean 对象
     * @return 转化出来的 Map 对象
     * @exception
     * java.beans.IntrospectionException - 如果分析类属性失败
     * java.lang.IllegalAccessException - 如果实例化 JavaBean 失败
     * java.lang.reflect.InvocationTargetException - 如果调用属性的 setter 方法失败
     * */
    public static Map convertBean(Object bean) throws IntrospectionException, IllegalAccessException, InvocationTargetException {
        Class type = bean.getClass();
        Map returnMap = new HashMap();
        BeanInfo beanInfo = Introspector.getBeanInfo(type);
        PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();

        for(int i = 0; i < propertyDescriptors.length; ++i) {
            PropertyDescriptor descriptor = propertyDescriptors[i];
            String propertyName = descriptor.getName();
            if(!propertyName.equals("class")) {
                Method readMethod = descriptor.getReadMethod();
                Object result = readMethod.invoke(bean, new Object[0]);
                if(result != null) {
                    returnMap.put(propertyName, StringUtil.toString(result));
                } else {
                    returnMap.put(propertyName, "");
                }
            }
        }

        return returnMap;
    }

    
    public static Object getObject(Map map, Object key, Object defaultValue, Class clazz) {
        return clazz == Double.class?getDouble(map, key, (Double)defaultValue):(clazz == Integer.class?getInteger(map, key, (Integer)defaultValue):(clazz == Long.class?getLong(map, key, (Long)defaultValue):(clazz == Float.class?getFloat(map, key, (Float)defaultValue):(clazz == Short.class?getShort(map, key, (Short)defaultValue):(clazz == String.class?getString(map, key, (String)defaultValue):(clazz == Byte.class?getByte(map, key, (Byte)defaultValue):(clazz == Map.class?getMap(map, key, (Map)defaultValue):(clazz == Boolean.class?getBoolean(map, key, (Boolean)defaultValue):(clazz == Number.class?getNumber(map, key, (Number)defaultValue):getObject(map, key, defaultValue))))))))));
    }

    
    /**
     * 从Map中获取指定参数，返回仅包含指定key的Map 
     * @param paramMap - 参数对象
     * @param properties - 包含要获取Key的Map(使用该Map作为返回对象)
     * */
    public static Map getDefineAttr(Map paramMap, Map properties) {
        Set keySet = properties.keySet();
        Iterator var3 = keySet.iterator();

        while(var3.hasNext()) {
            Object key = var3.next();
            properties.put(key, paramMap.get(key));
        }

        return properties;
    }

    
    /**
     * 从Map中获取指定参数，返回仅包含指定key的Map 
     * @param paramMap - 参数对象
     * @param properties - 包含要获取Key的数组
     * */
    public static Map getDefineAttr(Map paramMap, String[] properties) {
        Class clazz = paramMap.getClass();

        Object o;
        try {
            o = clazz.newInstance();
        } catch (Exception var9) {
            throw new RuntimeException(var9);
        }

        Map m = (Map)o;
        String[] var5 = properties;
        int var6 = properties.length;

        for(int var7 = 0; var7 < var6; ++var7) {
            String key = var5[var7];
            m.put(key, paramMap.get(key));
        }

        return m;
    }

    
    /**
     * 从Map中获取指定参数，返回仅包含指定key的Map 
     * @param listData - 参数对象
     * @param properties - 包含要获取Key的数组
     * */
    public static List<Map> getDefineAttr(List<Map> listData, String[] properties) {
        Class listClazz = listData.getClass();

        Object list;
        try {
            list = listClazz.newInstance();
        } catch (Exception var15) {
            throw new RuntimeException(var15);
        }

        List l = (List)list;
        Iterator var5 = listData.iterator();

        while(var5.hasNext()) {
            Map paramMap = (Map)var5.next();
            Class mapClazz = paramMap.getClass();

            Object map;
            try {
                map = mapClazz.newInstance();
            } catch (Exception var14) {
                throw new RuntimeException(var14);
            }

            Map m = (Map)map;
            String[] var10 = properties;
            int var11 = properties.length;

            for(int var12 = 0; var12 < var11; ++var12) {
                String key = var10[var12];
                m.put(key, paramMap.get(key));
            }

            l.add(m);
        }

        return l;
    }

    
    /**
     * 判断来自Request的参数集合中对应key值是否为true
     * @param paramMap - 参数集合
     * @param paramName - key
     * @return 是否为true
     * */
    public static boolean isTrueFromRequestParam(Map paramMap, String paramName) {
        String value = getString(paramMap, paramName);
        return value != null && (value.equalsIgnoreCase("true") || value.equalsIgnoreCase("t") || value.equalsIgnoreCase("1") || value.equalsIgnoreCase("enabled") || value.equalsIgnoreCase("y") || value.equalsIgnoreCase("yes") || value.equalsIgnoreCase("on"));
    }
}
